{% import _self as self %}

<div class="content">
    <h1>Upload Autograding Config</h1>
    <br><br>
    <p>
        Following the assignment configuration specifications:<br>
        <a href="http://submitty.org/instructor/assignment_configuration">
            http://submitty.org/instructor/assignment_configuration</a><br>
        and examples:<br>
        <a target=_blank href="https://github.com/Submitty/Tutorial/tree/master/examples">Submitty Tutorial example autograding configurations</a><br>
        <a target=_blank href="https://github.com/Submitty/Submitty/tree/master/more_autograding_examples">Additional example autograding configurations</a><br>
    </p>

    <br><br>
    <p>
        Prepare your autograding configuration as a single <code>config.json</code> file.<br>
        Or as a zip of the <code>config.json</code>, and the directories <code>provided_code</code>,
        <code>test_input</code>, <code>instructor_solution</code>, <code>test_output</code>, and/or <code>custom_validation_code</code>.
    </p>

    <br><br>

    <form action="{{ upload_url }}" method="POST" enctype="multipart/form-data">
        <input type="hidden" name="csrf_token" value="{{ csrf_token }}" />
        Upload Config: <input type="file" name="config_upload" /><br />
        <input type="submit" value="Upload" />
    </form>
</div>

{% if all_files|length > 0 %}
<div class="content">
    <h2>Previous Uploads</h2>
    <br>
    <b>located in {{ target_dir }}</b>
    <br>&nbsp;<br>
    <script>
        function openRenamePopup(file_path){
            let url = buildCourseUrl(['autograding_config', 'usage']) + '?config_path=' + encodeURIComponent(file_path);
            $('#alert_in_use').html('');
            $('#gradeables_using_config').empty();
            $.ajax({
                url : url,
                success(data){
                    var gradeable_ids = JSON.parse(data)['data'];
                    if(gradeable_ids.length > 0){
                        $('#alert_in_use').html("Note: Currently these gradeables are using this config, rename at your own risk");
                        for(var i = 0; i < gradeable_ids.length; i++){
                            $('#gradeables_using_config').append("<li>" + gradeable_ids[i] + "</li>");
                        }
                    }
                }
            });
            $('#rename_config_popup').css('display', 'block');
            $('#curr_config_name').val(file_path);
        }

        function openDeletePopup(file_path) {
            let message = $('[name="delete-config-message"]');
            message.html('');
            message.append('<b>'+file_path+'</b>');
            $('[name="config_path"]').val(file_path);
            $('#delete_config_popup').css("display", "block");
        }
    </script>
    <ul>
        {{ self.display_files(self, all_files, 1, false, inuse_config, display_url) }}
    </ul>
</div>
{% endif %}

{% macro display_files(self, files, indent, seen_root, inuse_config, display_url) %}
    {% set margin_left = 15 %}
    {% set neg_margin_left = -15 * indent %}
    {% for name, file in files %}
        {% set id = file.path | trim('/', 'right') | replace({'/': '_', '.': '_'}) %}
        {% if file.files != null %}
            <div>
                {% if indent == 1 %}
                    <a class="fas fa-pencil-alt" onclick="openRenamePopup('{{ file.path }}')"></a>
                    {% if file.path not in inuse_config %}
                        <a class="fas fa-trash" onclick="openDeletePopup('{{ file.path }}')"></a>
                    {% endif %}
                {% endif %}
                <span id='{{ id }}-span' class='fa icon-folder-closed'></span><a onclick='openDiv("{{ id }}");'>{{ seen_root ? name : file.path }}</a>
                <div id='{{ id }}' style='margin-left: {{ margin_left }}px; display: none'>
                    {{ self.display_files(self, file.files, indent + 1, true, inuse_config, display_url) }}
                </div>
            </div>
        {% else %}
            <div>
                <div class="file-viewer"><a onclick='openFrame("{{ display_url }}?dir=config_upload&path={{ file.path | url_encode }}", "{{ id }}", "{{ file.name }}")'>
                    <span class='icon-plus'></span>{{ file.name }}</a> <a onclick='openUrl("{{ display_url }}?dir=config_upload&path={{ file.path | url_encode }}")'>(Popout)</a>
                </div>
                <div id="file_viewer_{{ id }}" style='margin-left: {{ neg_margin_left }}px'></div>
            </div>
        {% endif %}
    {% endfor %}
{% endmacro %}

{% include('admin/RenameConfigPopup.twig')%}
{% include('admin/DeleteConfigPopup.twig')%}
